package com.opgea.crm.repository.security;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.crm.domain.entities.security.Group;
@Repository
public class GroupRepositoryImpl implements GroupRepository{

	@PersistenceContext
	EntityManager em;
	
	@Autowired
	RoleRepository roleRepository;	
	
	
	@Transactional
	public void create(Group group) {
		em.persist(group);
	}

	
	public Group find(Long id) {
		return em.find(Group.class, id);
	}

	
	public List<Group> findAll() {
		String query = "SELECT g FROM Group g";
		List<Group> groups = em.createQuery(query, Group.class).getResultList();
		System.out.println("Group List Size : "+groups.size());
		return groups;
	}



	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public void update(Group group) {
		if (em.find(Group.class, group.getId()) == null) {
	        throw new IllegalArgumentException("Unknown Group Id: " + group.getId());
	      }
		Group managedGroup = em.merge(group);
		System.out.println("Managed Group : "+managedGroup);
	}

	@Override
	public void remove(Long id) {
	}

}
